package net.jueb.protoServer.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;

import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.MessageLite;

import demo.protocol.MessageTestProto;
import net.jueb.protoServer.handler.codec.impl.ProtoMessageDecoderHandler;
import net.jueb.protoServer.handler.codec.impl.ProtoMessageEncoderHandler;
import net.jueb.protoServer.handler.codec.impl.ProtoMessagePackHandler;
import net.jueb.protoServer.handler.codec.impl.ProtoMessageUnPackHandler;
import net.jueb.protoServer.handler.dispatcher.MessageDispatcherHandler;
import net.jueb.protoServer.handler.dispatcher.ProtoMessageDispatcherHandler;
import net.jueb.protoServer.message.ProtoMessage;
import net.jueb.protoServer.message.mapping.MessageMapping;
import net.jueb.protoServer.message.mapping.ProtoMessageMapping;

/**
 * 使用ProtoMessage.Message消息类型的端口传输协议
 * @author Administrator
 *
 */
public class ProtoMessageProtocol extends PortProtocol<ProtoMessage.Message>{

	private final MessageLite prototype=ProtoMessage.Message.getDefaultInstance();
	private final ExtensionRegistry registry=ExtensionRegistry.newInstance();
	
	{
		//注册消息类型
		ProtoMessage.registerAllExtensions(registry);
		MessageTestProto.registerAllExtensions(registry);//注册测试扩展消息类型
	}
	
	public ExtensionRegistry getRegistry()
	{
		return this.registry;
	}
	
	@Override
	public MessageMapping<ProtoMessage.Message> getMessageMapping() {
		return new ProtoMessageMapping();
	}

	@Override
	public MessageDispatcherHandler<ProtoMessage.Message> getMessageDispatcher() {
		return new ProtoMessageDispatcherHandler(controllers);
	}

	@Override
	public ByteToMessageDecoder getMessageUnPackHandler() {
		return new ProtoMessageUnPackHandler();
	}

	@Override
	public MessageToMessageDecoder<ByteBuf> getMessageDecoderHandler() {
		return new ProtoMessageDecoderHandler(this.prototype, this.registry);
	}

	@Override
	public MessageToByteEncoder<ByteBuf> getMessagePackHandler() {
		return new ProtoMessagePackHandler();
	}

	@Override
	public MessageToMessageEncoder<?> getMessageEncoderHandler() {
		return new ProtoMessageEncoderHandler();
	}

}
